import java.util.concurrent.TimeUnit;

public class ThreadJoin {
    public static void main(String[] args) throws InterruptedException {
        Thread thread1=new Thread(() -> {
            for(int i=1;i<=5;i++){
                System.out.println(Thread.currentThread().getName()+":"+i); }
        },"线程1");
        Thread thread2=new Thread(new Runnable() {
            @Override
            public void run() {
                for(int i=1;i<=5;i++){
                    System.out.println(Thread.currentThread().getName()+":"+i);
                }
            }
        },"线程2");

        //启动线程1
        thread1.start();
        thread1.sleep(1000);
        //启动线程2
        thread2.start();

        /**
         *  当代码执行到这一步的时候，程序不再向下执行，等待thread1执行完毕后向下执行
         *  同时thread1和thread2相互挣多cpu调度资源
         */
       thread1.join();


/*        for(int i=0;i<2;i++){
            System.out.println(Thread.currentThread().getName()+"#"+i);
            TimeUnit.SECONDS.sleep(1);
        }*/
        /**
         * 当代码执行到这一步的时候，程序不在向下执行，等待thread2执行完毕后向下执行
         * 因为此时的thread1已经执行完毕了，只用thread2执行
         */
        thread2.join();
        
        for(int i=0;i<2;i++){
            System.out.println(Thread.currentThread().getName()+"#"+i);
            TimeUnit.SECONDS.sleep(1);
        }
    }

}